package com.liutaw.nanjingair.views.viewcomponent.weather;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

import com.liutaw.nanjingair.R;
import com.liutaw.nanjingair.utils.FileUtils;
import com.liutaw.nanjingair.views.viewcomponent.ViewUtils;
import com.szjlxh.domain.model.index.WeatherSevenDay;

import java.util.List;

/**
 * Created by liutao on 16/2/17.
 */
public class WeatherReport extends View {
    private static int NUMSPERPAGE = 6;
    private int deviceHeight;
    private int deviceWidth;
    private float singleHourWidth;
    private float totalWidth;

    private int pieces;
    private Paint titlePaint;//标题
    private Paint dividerPaint;//分割线
    private Paint timeLight;//折线顶部的信息
    private Paint topInfo;//折线顶部的信息
    private Paint highDegreePaint;//高温折线
    private Paint lowDegreePaint;//低温折线
    private Paint degreeTextPaint;//圆点

    //data
    private int[] highDegreesData = {6, 4, 5, 6, 8, 15, 10};
    private int[] lowDegreesData = {-2, -3, 3, 5, 5, -9, 3};
    private List<WeatherSevenDay> weatherSevenDayList;
    //    private WeatherSevenDayBean weatherDayInfoBean;
    private float textSize;
    private float circleSize;
    private int smallTextColor;
    private float normalDivider;

    public WeatherReport(Context context) {
        super(context);
        init();
    }

    public WeatherReport(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.WeatherReport);
        textSize = ta.getDimension(R.styleable.WeatherReport_textSize, 10);
        circleSize = ta.getDimension(R.styleable.WeatherReport_circleSize, 2);
        smallTextColor = ta.getColor(R.styleable.WeatherReport_smallTextColor, 0xFFFFFF);
        pieces = ta.getInteger(R.styleable.WeatherReport_count, WeatherReport.NUMSPERPAGE);
        ta.recycle();
        init();
    }

    private void init() {
//        height = (int) getContext().getResources().getDimension(R.dimen.chart_height);    //图片的高度,横坐标就是屏幕的宽度，纵坐标就是高度

        //divider
        normalDivider = ViewUtils.dip2px(getContext(), 8);
        dividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        dividerPaint.setColor(Color.parseColor("#4dffffff"));
        dividerPaint.setStrokeWidth(1);

        //title
        titlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        titlePaint.setColor(Color.WHITE);
        titlePaint.setTextSize(ViewUtils.dip2px(getContext(), 14));
        titlePaint.setTextAlign(Paint.Align.CENTER);

        timeLight = new Paint(Paint.ANTI_ALIAS_FLAG);
        timeLight.setColor(Color.parseColor("#80FFFFFF"));
        timeLight.setTextSize(textSize);
        timeLight.setTextAlign(Paint.Align.CENTER);

        //top info
        topInfo = new Paint(Paint.ANTI_ALIAS_FLAG);
        topInfo.setColor(smallTextColor);
        topInfo.setTextSize(textSize);
        topInfo.setTextAlign(Paint.Align.CENTER);
        //high line
        highDegreePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        highDegreePaint.setColor(Color.parseColor("#F87275"));
        highDegreePaint.setStrokeWidth(4);
        highDegreePaint.setAntiAlias(true);
        //low line
        lowDegreePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        lowDegreePaint.setColor(Color.parseColor("#96D4FD"));
        lowDegreePaint.setStrokeWidth(4);
        //degree text
        degreeTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        degreeTextPaint.setTextSize(ViewUtils.dip2px(getContext(), 10));
        degreeTextPaint.setColor(Color.WHITE);
        degreeTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (weatherSevenDayList == null || weatherSevenDayList.size() == 0) {
            return;
        }

        //绘制标题
        float normalDivider = ViewUtils.dip2px(getContext(), 8);
//        canvas.drawText("未来五天预报", normalDivider, getTop(), titlePaint);
//        Paint.FontMetrics titleFontMetrics = titlePaint.getFontMetrics();
        //坐标从(0,top)开始到(width,top+height)结束
//        float top = this.getTop();
        float top = 0;
        int height = getMeasuredHeight();
        int width = getMeasuredWidth();
        //首先分为6等份
        int eachWidth = width / pieces;

        //绘制分隔的白线
        canvas.drawLine(0, top, width, top, dividerPaint);
        for (int i = 1; i <= pieces; i++) {
            canvas.drawLine(eachWidth * i, top, eachWidth * i, top + height, dividerPaint);
        }
        //绘制折线顶部的信息
        int startX = eachWidth / 2;
        top += normalDivider * 3;
        int bitmapHeight = 0;
        for (int i = 0; i <= pieces - 1; i++) {
            float leftX = startX + eachWidth * i;
            //TODO 这里显示星期几的数据
            canvas.drawText(weatherSevenDayList.get(i).getWeekDay(), leftX, top, titlePaint);//星期
            float tempTop = top + titlePaint.getTextSize() + normalDivider;
            String monthday = weatherSevenDayList.get(i).getMonthDay();
            canvas.drawText(monthday, leftX, tempTop, timeLight);//日期
            tempTop += timeLight.getTextSize() + normalDivider;
            Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), FileUtils.getResId(getContext(),weatherSevenDayList.get(i).getIconLastName()));
            bitmapHeight = bitmap.getHeight();
            canvas.drawBitmap(bitmap, leftX - bitmap.getWidth() / 2, tempTop, null);
//            canvas.drawText(weatherDayInfos.get(i).getMonthday(), leftX, tempTop, topInfo);//天气
        }
        top += topInfo.getTextSize() * 2 + normalDivider * 4 + bitmapHeight + highDegreePaint.getTextSize();
        //绘制折线
        int maxDegreeHigh = highDegreesData[0];
        for (int i = 1; i < highDegreesData.length; i++) {
            if (maxDegreeHigh < highDegreesData[i]) {
                maxDegreeHigh = highDegreesData[i];
            }
        }
//        int maxDegreeLow = lowDegreesData[0];
        int minDegreeLow = lowDegreesData[0];
        for (int i = 1; i < lowDegreesData.length; i++) {
            if (minDegreeLow > lowDegreesData[i]) {
                minDegreeLow = lowDegreesData[i];
            }
        }
        int degressLengh = maxDegreeHigh - minDegreeLow;
        int lineHeight = ViewUtils.dip2px(getContext(), 100);
        double one = lineHeight / degressLengh;//刻度

        for (int i = 0; i < pieces; i++) {
            //绘制圆点
            canvas.drawCircle(startX + eachWidth * i, (float) (top + (maxDegreeHigh - highDegreesData[i]) * one), circleSize, highDegreePaint);
            canvas.drawText(highDegreesData[i] + "°", startX + eachWidth * i, (float) (top + (maxDegreeHigh - highDegreesData[i]) * one - normalDivider), degreeTextPaint);
            if (i == pieces - 1) break;
            //绘制折线
            canvas.drawLine(startX + eachWidth * i, (float) (top + (maxDegreeHigh - highDegreesData[i]) * one), startX + eachWidth * (i + 1), (float) (top + (maxDegreeHigh - highDegreesData[i + 1]) * one), highDegreePaint);
        }

        for (int i = 0; i < pieces; i++) {
            //绘制圆点
            canvas.drawCircle(startX + eachWidth * i, (float) (top + (maxDegreeHigh - lowDegreesData[i]) * one), circleSize, lowDegreePaint);
            canvas.drawText(lowDegreesData[i] + "°", startX + eachWidth * i, (float) (top + (maxDegreeHigh - lowDegreesData[i]) * one + normalDivider * 2), degreeTextPaint);
            if (i == pieces - 1) break;
            //绘制折线
            canvas.drawLine(startX + eachWidth * i, (float) (top + (maxDegreeHigh - lowDegreesData[i]) * one), startX + eachWidth * (i + 1), (float) (top + (maxDegreeHigh - lowDegreesData[i + 1]) * one), lowDegreePaint);
        }
        //绘制折线底部的信息
        top += lineHeight + normalDivider * 3 + lowDegreePaint.getTextSize();
        for (int i = 0; i <= pieces - 1; i++) {
            float leftX = startX + eachWidth * i;

//            Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), FileUtils.getResId(getContext(),weatherSevenDayList.get(i).getIconLastName()));
//            bitmapHeight = bitmap.getHeight();
//            canvas.drawBitmap(bitmap, leftX - bitmap.getWidth() / 2, top, null);//天气
            float tempTop = top  + normalDivider * 2;
            canvas.drawText(weatherSevenDayList.get(i).getWeatherName(), leftX, tempTop, topInfo);//描述
            tempTop += topInfo.getTextSize() + normalDivider;
            canvas.drawText(weatherSevenDayList.get(i).getWindName(), leftX, tempTop, topInfo);//风
            tempTop += topInfo.getTextSize() + normalDivider;
            canvas.drawText(weatherSevenDayList.get(i).getWindScale(), leftX, tempTop, topInfo);//范围
        }
//        top += topInfo.getTextSize() * 3 + normalDivider * 4 + bitmapHeight;
//        ViewGroup.LayoutParams layoutParams = this.getLayoutParams();
//        layoutParams.width = width;
//        layoutParams.height = (int) top+1;
//        this.setLayoutParams(layoutParams);
        super.onDraw(canvas);
//        setMeasuredDimension(width, (int) top+1);

    }


    public void setWeatherDayInfos(List<WeatherSevenDay> weatherSevenDayList) {
        this.weatherSevenDayList = weatherSevenDayList;
        highDegreesData = new int[weatherSevenDayList.size()];
        lowDegreesData = new int[weatherSevenDayList.size()];
        for (int i = 0; i < weatherSevenDayList.size(); i++) {
            highDegreesData[i] = weatherSevenDayList.get(i).getMaxTemp();
            lowDegreesData[i] = weatherSevenDayList.get(i).getMingTemp();
        }

        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        deviceHeight = displayMetrics.heightPixels;
        deviceWidth = displayMetrics.widthPixels;

        singleHourWidth = deviceWidth / WeatherReport.NUMSPERPAGE;
        totalWidth = singleHourWidth * pieces;
//        WindowManager wm = (WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE);
//        Display display = wm.getDefaultDisplay();
        Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.d00);
        int height = (int) (normalDivider * 16 + ViewUtils.dip2px(getContext(), 100) + bitmap.getHeight() * 1 + topInfo.getTextSize() * 5);
        setMeasuredDimension((int) totalWidth, height);
    }
}
